/*
 * ItemImage.java
 *
 *  created: 21.8.2011
 *  charset: UTF-8
 *  license: MIT (X11) (See LICENSE file for full license)
 */
package cz.mp.k3bg.core;

import cz.mp.k3bg.log.LoggerManager;
import cz.mp.util.FileUtils;
import java.io.File;
import java.util.logging.Logger;

/**
 * Třída {@code ItemImage} obsahuje informace o obrázku.
 * 
 * @author Martin Pokorný
 * @version 0.1
 * @see Item
 * @see HtmlFile
 */
public class ItemImage implements Comparable, Item {

    private static final boolean DEBUG = false;
    private static final Logger logger =
            LoggerManager.getLogger(ItemImage.class, DEBUG);
    
    private String path;
//    private String alt;     // alt se nikde nepoužívá, nikde není třeba...
    private int width;
    private int height;
    
    /** Identifikace v {@code opf} souboru. */
    private String id = "";
    
    // -----
    
    /** */
    public ItemImage(String path) {  
        setPathImpl(path);
    }

    
    /** */
    public ItemImage(String path, int width, int height) {  
        this(path);
        logger.finer("width = " + width);
        this.width = width;
        logger.finer("height = " + height);
        this.height = height;
    }    
    
    // -----
    
    // (pro tento program postačí )
    /**
     * Získá mime typ pro soubory s příponou: 
     * {@literal png,jpeg,jpg,jpe,gif,bmp}.
     * 
     * @return 
     * @throws IllegalStateException  pokud ItemImage nemá podporovanou příponu.
     */
    @Override
    public MimeType getMimeType() {
        String ext = FileUtils.getFileExtension(path);
        MimeType result = null;
        if (ext.equalsIgnoreCase("png")) {
            result = MimeType.PNG;
        }
        else if (ext.equalsIgnoreCase("jpeg")  
                || ext.equalsIgnoreCase("jpg") 
                || ext.equalsIgnoreCase("jpe")) {
            result = MimeType.JPEG;
        }
        else if (ext.equalsIgnoreCase("gif")) {
            result = MimeType.GIF;
        }
        else if (ext.equalsIgnoreCase("bmp")) {
            result = MimeType.BMP;
        }
        else {
            throw new IllegalStateException("unsuported " + ext);
        }
        logger.finer("result = " + result + "   path = " + path);
        return result;
    }
    
    /**
     * Získá dříve určenou identifikaci v {@code opf} souboru.
     * 
     * @return 
     */
    @Override
    public String getId() {
        return id;
    }

    /**
     * Nastaví Identifikaci v {@code opf} souboru.
     *
     * @param id 
     */
    @Override
    public void setId(String id) {
        if (id == null) {
            throw new IllegalArgumentException("id=null");
        }
        logger.finer("id = " + id);
        this.id = id;
    }
    
    public static final int KINDLE_COVER_WIDTH_MAX = 600;
    public static final int KINDLE_COVER_WIDTH_MIN = 500;
    public static final int KINDLE_COVER_HEIGHT = 800;
    
    /**
     * Zjistí zda má obrázek dobrou velikost na to, 
     * aby dobře vypadal jako obálka knihy v Amazon Kindle 3.
     * 
     * @param width
     * @param height
     * @return 
     */
    public static boolean isGoodKindle3BookCover(int width, int height) {
        if (width <= KINDLE_COVER_WIDTH_MAX 
                && width >= KINDLE_COVER_WIDTH_MIN 
                && height == KINDLE_COVER_HEIGHT) {
            return true;
        }        
        return false;
    }

//    /**
//     * 
//     * @return 
//     */
//    private String createAltAlt() {
//        return "[" + FileUtils.getFileBaseName(path) + "]";
//    }

    /**
     * 
     * @return 
     */
    public int getHeight() {
        return height;
    }

    /**
     * 
     * @param height 
     */
    public void setHeight(int height) {
        logger.finer("height = " + height);
        this.height = height;
    }

    
    /**
     * 
     * @return 
     */
    public int getWidth() {
        return width;
    }
    
    /**
     * 
     * @param width 
     */
    public void setWidth(int width) {
        logger.finer("width = " + width);
        this.width = width;
    }

    /**
     * 
     * @return 
     */
    @Override
    public String getPath() {
        return path;
    }

    /**
     * 
     * @param path 
     * @throws IllegalArgumentException  pokud {@literal path = null}
     */
    public void setPath(String path) {
        setPathImpl(path);
    }
    
    /**
     * 
     * @param path 
     * @throws IllegalArgumentException  pokud {@literal path = null}
     */
    private void setPathImpl(String path) {
        if (path == null) {
            throw new IllegalArgumentException("path=null");
        }
        logger.finer("path = " + path);
        this.path = path;
    }
  
    
    /**
     * 
     * @param toDir
     * @return 
     */
    @Override
    public String getRelativePath(String toDir) {
        if (new File(getPath()).isAbsolute()) {
            return FileUtils.relativizePath(toDir, getPath());
        }
        return getPath();
    } 

    @Override
    public String getAbsolutePath() {
        return FileUtils.getAbsoluteFilePath(new File(getPath()));
    }
    
    /**
     * Testuje ekvivalenci objektů {@code ItemImage}.
     * Dva objekty {@code ItemImage} jsou považovány za totožné, pokud 
     * mají stejnou zadánu cestu v souborovém systému.
     * 
     * @param obj
     * @return 
     */
    // (generováno s Netbeans)
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ItemImage other = (ItemImage) obj;
        if ((this.path == null) ? 
                (other.path != null) : !this.path.equals(other.path)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 11 * hash + (this.path != null ? this.path.hashCode() : 0);
        return hash;
    }
    

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("path=").append(path);
        if (width > 0) {
            sb.append("  width=").append(width);
        }
        if (height > 0) {
            sb.append("  height=").append(height);
        }    
        sb.append("  ");
        return sb.toString();
    }

    @Override
    public int compareTo(Object obj) {
        final ItemImage other = (ItemImage) obj;
        return this.path.compareTo(other.path);
    }
    
}   // ItemImage
